
import glob
import re
import string
import codecs
import pepe.p_txt.bigtxt as bt

n = '\x0D\x0A'

def main():
    ''' '''
    pf_masks = ["""*.csv"""]

    for pf_mask in pf_masks:
        #print pf_mask

        set_last_column_width = 30
        reference_data_line_length = 0
        reference_header = ''
        len_line = 0

        for f in glob.glob(pf_mask):

            fname = string.split(f, '\x5C')[-1]
            if fname in ('xxx'):
                pass
            else:
                print fname
                o = open(f + '.added-end-space', 'wb')
                lino = 0
                for line in bt.lines(f):
                    lino += 1
                    len_line = len(line)

                    if lino == 1 and reference_header == '':
                        reference_header = line

                    if lino == 2 and reference_data_line_length == 0:
                        reference_data_line_length = len_line

                    # checking for difference in headers
                    if lino == 1 and line <> reference_header:
                        raise 'header is different', f

                    # checking for longer line than reference line + set_last_column_width
                    if len_line > (reference_data_line_length + set_last_column_width) and lino <> 1:
                        rtext = string.join([line, str(reference_data_line_length)], ' ')
                        raise 'actual line length is longer than ' + \
                        'reference_data_line_length + set_last_column width ', rtext

                    # checking for shorter line than reference line
                    if len_line < reference_data_line_length and lino <> 1:
                        rtext = string.join([line, str(reference_data_line_length)], ' ')
                        raise 'actual line length is shorter than ' + \
                        'reference_data_line_length', rtext
    
                    #line = re.sub('\x0D', '', line)
                    # faster alternative than re.sub
                    if line[-1] == '\x0A': line = line[:-1]
                    #line = re.sub('\x0A', '', line)
                    #line = re.sub(n, '', line)
    
                    #line = line + ' '
                    padding_diff = len_line - reference_data_line_length
                    if padding_diff == 0: line = line + ' ' * set_last_column_width
                    else: line = line + ' ' * (set_last_column_width - padding_diff)
                    
                    o.write(line + n)
                o.close()

        reference_data_line_length = 0
main()